package com.itheima.web.servlet;

import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(value = "/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Book book = new Book();

            BeanUtils.populate(book,parameterMap);

            BookService bookService = new BookService();
            bookService.add(book);

            //重定向到findAll
            response.sendRedirect(request.getContextPath() +"/findAll");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
